package com.apollographql.apollo.interceptor;

import com.apollographql.apollo.api.Operation;
import com.apollographql.apollo.api.internal.Optional;
import com.apollographql.apollo.api.internal.Utils;
import com.apollographql.apollo.cache.CacheHeaders;
import com.apollographql.apollo.cache.normalized.Record;
import com.apollographql.apollo.exception.ApolloException;
import com.apollographql.apollo.request.RequestHeaders;
import java.util.Collection;
import java.util.UUID;
import java.util.concurrent.Executor;
import okhttp3.Response;

public interface ApolloInterceptor {

    public interface CallBack {
        void onCompleted();

        void onFailure(ApolloException apolloException);

        void onFetch(FetchSourceType fetchSourceType);

        void onResponse(InterceptorResponse interceptorResponse);
    }

    public enum FetchSourceType {
        CACHE,
        NETWORK
    }

    void dispose();

    void interceptAsync(InterceptorRequest interceptorRequest, ApolloInterceptorChain apolloInterceptorChain, Executor executor, CallBack callBack);

    public static final class InterceptorResponse {
        public final Optional<Collection<Record>> cacheRecords;
        public final Optional<Response> httpResponse;
        public final Optional<com.apollographql.apollo.api.Response> parsedResponse;

        public InterceptorResponse(Response httpResponse2) {
            this(httpResponse2, null, null);
        }

        public InterceptorResponse(Response httpResponse2, com.apollographql.apollo.api.Response parsedResponse2, Collection<Record> cacheRecords2) {
            this.httpResponse = Optional.fromNullable(httpResponse2);
            this.parsedResponse = Optional.fromNullable(parsedResponse2);
            this.cacheRecords = Optional.fromNullable(cacheRecords2);
        }
    }

    public static final class InterceptorRequest {
        public final boolean autoPersistQueries;
        public final CacheHeaders cacheHeaders;
        public final boolean fetchFromCache;
        public final Operation operation;
        public final Optional<Operation.Data> optimisticUpdates;
        public final RequestHeaders requestHeaders;
        public final boolean sendQueryDocument;
        public final UUID uniqueId = UUID.randomUUID();
        public final boolean useHttpGetMethodForQueries;

        InterceptorRequest(Operation operation2, CacheHeaders cacheHeaders2, RequestHeaders requestHeaders2, Optional<Operation.Data> optimisticUpdates2, boolean fetchFromCache2, boolean sendQueryDocument2, boolean useHttpGetMethodForQueries2, boolean autoPersistQueries2) {
            this.operation = operation2;
            this.cacheHeaders = cacheHeaders2;
            this.requestHeaders = requestHeaders2;
            this.optimisticUpdates = optimisticUpdates2;
            this.fetchFromCache = fetchFromCache2;
            this.sendQueryDocument = sendQueryDocument2;
            this.useHttpGetMethodForQueries = useHttpGetMethodForQueries2;
            this.autoPersistQueries = autoPersistQueries2;
        }

        public Builder toBuilder() {
            return new Builder(this.operation).cacheHeaders(this.cacheHeaders).requestHeaders(this.requestHeaders).fetchFromCache(this.fetchFromCache).optimisticUpdates(this.optimisticUpdates.orNull()).sendQueryDocument(this.sendQueryDocument).useHttpGetMethodForQueries(this.useHttpGetMethodForQueries).autoPersistQueries(this.autoPersistQueries);
        }

        public static Builder builder(Operation operation2) {
            return new Builder(operation2);
        }

        public static final class Builder {
            private boolean autoPersistQueries;
            private CacheHeaders cacheHeaders = CacheHeaders.NONE;
            private boolean fetchFromCache;
            private final Operation operation;
            private Optional<Operation.Data> optimisticUpdates = Optional.absent();
            private RequestHeaders requestHeaders = RequestHeaders.NONE;
            private boolean sendQueryDocument = true;
            private boolean useHttpGetMethodForQueries;

            Builder(Operation operation2) {
                this.operation = (Operation) Utils.checkNotNull(operation2, "operation == null");
            }

            public Builder cacheHeaders(CacheHeaders cacheHeaders2) {
                this.cacheHeaders = (CacheHeaders) Utils.checkNotNull(cacheHeaders2, "cacheHeaders == null");
                return this;
            }

            public Builder requestHeaders(RequestHeaders requestHeaders2) {
                this.requestHeaders = (RequestHeaders) Utils.checkNotNull(requestHeaders2, "requestHeaders == null");
                return this;
            }

            public Builder fetchFromCache(boolean fetchFromCache2) {
                this.fetchFromCache = fetchFromCache2;
                return this;
            }

            public Builder optimisticUpdates(Operation.Data optimisticUpdates2) {
                this.optimisticUpdates = Optional.fromNullable(optimisticUpdates2);
                return this;
            }

            public Builder optimisticUpdates(Optional<Operation.Data> optimisticUpdates2) {
                this.optimisticUpdates = (Optional) Utils.checkNotNull(optimisticUpdates2, "optimisticUpdates == null");
                return this;
            }

            public Builder sendQueryDocument(boolean sendQueryDocument2) {
                this.sendQueryDocument = sendQueryDocument2;
                return this;
            }

            public Builder useHttpGetMethodForQueries(boolean useHttpGetMethodForQueries2) {
                this.useHttpGetMethodForQueries = useHttpGetMethodForQueries2;
                return this;
            }

            public Builder autoPersistQueries(boolean autoPersistQueries2) {
                this.autoPersistQueries = autoPersistQueries2;
                return this;
            }

            public InterceptorRequest build() {
                return new InterceptorRequest(this.operation, this.cacheHeaders, this.requestHeaders, this.optimisticUpdates, this.fetchFromCache, this.sendQueryDocument, this.useHttpGetMethodForQueries, this.autoPersistQueries);
            }
        }
    }
}
